package com.cz.video.system.service.impl;



import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cz.video.common.constant.CommonConstants;
import com.cz.video.common.core.domain.PageQuery;
import com.cz.video.common.core.domain.bo.SysSensitiveWordBo;
import com.cz.video.common.core.domain.entity.SysSensitiveWord;
import com.cz.video.common.core.domain.vo.SysSensitiveWordVo;
import com.cz.video.common.core.page.TableDataInfo;
import com.cz.video.common.core.service.SysSensitiveWordService;
import com.cz.video.common.utils.MapstructUtils;
import com.cz.video.common.utils.StringUtils;
import com.cz.video.common.utils.spring.SpringUtils;
import com.cz.video.system.mapper.SysSensitiveWordMapper;
import com.github.houbb.sensitive.word.bs.SensitiveWordBs;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 话题 服务实现类
 * </p>
 *
 * @author
 * @since 2023-07-06
 */
@Service
@AllArgsConstructor
public class SysSensitiveWordServiceImpl implements SysSensitiveWordService {
  private final SysSensitiveWordMapper sensitiveWordMapper;

//  @Lazy
//  @Autowired
//  private  SensitiveWordBs sensitiveWordBs;


  @Override
  public TableDataInfo<SysSensitiveWordVo> page(SysSensitiveWordBo sensitiveWord, PageQuery pageQuery) {
    Page<SysSensitiveWordVo> page = sensitiveWordMapper.selectPageList(pageQuery.build(), this.buildQueryWrapper(sensitiveWord));
    return TableDataInfo.build(page);
  }

  @Override
  public List<SysSensitiveWordVo> list(SysSensitiveWordBo sensitiveWordBo) {
    return sensitiveWordMapper.selectVoList(this.buildQueryWrapper(sensitiveWordBo));
  }

  @Override
  public SysSensitiveWordVo get(Long id) {
    return sensitiveWordMapper.selectVoById(id);
  }


  @Override
  public int add(SysSensitiveWordBo sensitiveWord) {
    SysSensitiveWord sysHashTag = MapstructUtils.convert(sensitiveWord, SysSensitiveWord.class);
    return sensitiveWordMapper.insert(sysHashTag);
  }

  @Override
  public int update(SysSensitiveWordBo sensitiveWord) {

    SysSensitiveWord sysHashTag = MapstructUtils.convert(sensitiveWord, SysSensitiveWord.class);

    return sensitiveWordMapper.updateById(sysHashTag);
  }

  @Override
  public int updateStatus(Long sensitiveWordId, String status) {
    return sensitiveWordMapper.updateStatus(sensitiveWordId, status);
  }

  @Override
  public int deleteByIds(Long[] ids) {
    return sensitiveWordMapper.deleteBatchIds(Collections.singleton(ids));
  }

    @Override
    public void refresh() {
        // 每次数据库的信息发生变化之后，首先调用更新数据库敏感词库的方法，然后调用这个方法。
        SpringUtils.getBean(SensitiveWordBs.class).init();
    }

  /**
   * 根据查询条件构建query wrapper
   *
   * @param sensitiveWord 查询条件
   *
   * @return query wrapper
   */
  private Wrapper<SysSensitiveWord> buildQueryWrapper(SysSensitiveWordBo sensitiveWord) {
      LambdaQueryWrapper<SysSensitiveWord> wrapper = Wrappers.lambdaQuery();
      wrapper.eq(SysSensitiveWord::getDelFlag, CommonConstants.NOT_DELETED)
          .eq(StringUtils.isNotBlank(sensitiveWord.getType()), SysSensitiveWord::getType, sensitiveWord.getType())
          .eq(StringUtils.isNotBlank(sensitiveWord.getStatus()), SysSensitiveWord::getStatus, sensitiveWord.getStatus())
          .like(StringUtils.isNotBlank(sensitiveWord.getWord()), SysSensitiveWord::getWord, sensitiveWord.getWord())
      ;
    return wrapper;
  }
}
